﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace SaintWinControl
{
    public partial class ControlMatrix : UserControl
    {

        [Category("SaintWinControl")]
        [Description("該控制項是否允許在新增模式下輸入")]
        public bool AllowInsert
        {
            get;
            set;
        }

        [Category("SaintWinControl")]
        [Description("該控制項是否允許在修改模式下輸入")]
        public bool AllowUpdate
        {
            get;
            set;
        }

        [Category("SaintWinControl")]
        [Description("該控制項是否允許在查詢模式下輸入")]
        public bool AllowQuery
        {
            set;
            get;
        }

        /// <summary>
        /// 如果有KEY值，不需要在form中出現，在異動時會需要用到，預設會出現，可以在
        /// </summary>
        [Category("SaintWinControl")]
        [Description("該控制項是否允許顯示"), DefaultValue(true)]
        public bool AllowVisible
        {
            set
            {
                allowVisible = value;
            }
            get
            {
                return allowVisible;
            }
        }

        protected bool allowVisible = true;
        protected string errorMsg = string.Empty;
        /// <summary>
        /// 設定要做Validation 的 Class 名
        /// </summary>
        [Category("SaintWinControl")]
        [Description("設定要做Validation 的 Class 名")]
        public string ValidationClass
        {
            get;
            set;
        }

        /// <summary>
        /// 指定的控制項
        /// </summary>
        protected Control accessControl
        {
            set
            {
                if (_accessControl == null)
                {
                    _accessControl = value;
                    SetFouckLeaveColor(Color.White);
                }
            }
            get
            {
                return _accessControl;
            }
        }

        protected Control _accessControl = null;

        public ControlMatrix()
        {
            InitializeComponent();

        }

        private void ControlMatrix_Enter(object sender, EventArgs e)
        {
            SetFouckLeaveColor(Color.Yellow);
        }

        private void ControlMatrix_Leave(object sender, EventArgs e)
        {
            SetFouckLeaveColor(Color.White);
        }

        protected void SetFouckLeaveColor(Color p_color)
        {
            if (accessControl != null)
            {
                accessControl.BackColor = p_color;
            }

        }

        public virtual void ClearText()
        {
            accessControl.Text = string.Empty;
        }

        /// <summary>
        /// 預設是字串格式!
        /// </summary>
        /// <param name="p_value"></param>
        public virtual void SetValue(object p_value)
        {
            //如果accessControl 不是Textbox 要變化嗎
            accessControl.Text = p_value.ToString();
        }

        protected override void Select(bool directed, bool forward)
        {
            //base.Select(directed, forward);
            accessControl.Focus();
        }

        /// <summary>
        /// 在這做資料正確性的檢查
        /// </summary>
        /// <returns></returns>
        public bool Validation(string p_assemblyPath)
        {
            errorMsg = string.Empty;
            bool result = false;
            if (!string.IsNullOrEmpty(ValidationClass))
            {
                try
                {                  
                    //string dll = @"D:\Work\PackingSys\bin\Packing.exe";
                    Assembly instance = Assembly.LoadFrom(p_assemblyPath);
                    Type t = null;
                    t = instance.GetType(ValidationClass);
                    iValidation val = System.Activator.CreateInstance(t)
                        as iValidation;
                    result = val.Validation(accessControl);
                }
                //type is Nothing. 這表示輸入的字串無法Reflection to object
                catch (ArgumentNullException argEx)
                {
                    errorMsg = "ArgumentNullException：" + argEx.Message;
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                }
            }
            return result;
        }


    }
}
